package com.common.weibolib;

import java.util.ArrayList;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.common.allCommon.CommUtil;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.starappallcommon.R;

public class TweetImageAdapter extends BaseAdapter {
	private Context mContext = null;
	private ArrayList<String> mThumbnail = null;
	private ArrayList<String> mImgExt = null;
	private int mImgWidth;
	private int mImgHeight;
	private boolean mIsLocalImg = false;

	protected ImageLoader mImageLoader = ImageLoader.getInstance();
	public TweetImageAdapter(Context context) {
		mContext = context;
	}

	@Override
	public int getCount() {
		return null == mThumbnail ? 0 : mThumbnail.size();
	}

	@Override
	public Object getItem(int position) {
		return null == mThumbnail ? null : mThumbnail.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// modify by chenqs
		ViewHolder holder;
		if (convertView == null) {
			convertView = View.inflate(mContext, R.layout.tweet_image_item,
					null);
			holder = new ViewHolder();
			holder.albumIv = (ImageView) convertView
					.findViewById(R.id.album_img);
			holder.ivGifSign = (ImageView) convertView
					.findViewById(R.id.gif_sign);
			holder.albumIv.getLayoutParams().width = mImgWidth;
			holder.albumIv.getLayoutParams().height = mImgHeight;
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
			if (holder.albumIv.getLayoutParams().width != mImgWidth
					|| holder.albumIv.getLayoutParams().height != mImgHeight) {
				convertView = View.inflate(mContext, R.layout.tweet_image_item,
						null);
				holder = new ViewHolder();
				holder.albumIv = (ImageView) convertView
						.findViewById(R.id.album_img);
				convertView.setTag(holder);
			}
		}
		if(CommUtil.isGifForWeibo(mImgExt.get(position), mThumbnail.get(position))){
			holder.ivGifSign.setVisibility(View.VISIBLE);
		}else{
			holder.ivGifSign.setVisibility(View.GONE);
		}
		if(mIsLocalImg){
			mImageLoader.displayImage(CommUtil.LOCAL_FILE_PREFIX + mThumbnail.get(position), holder.albumIv,
					GlobalSetting.getWeiboCacheOpt(mContext), GlobalSetting.getImageLoadingListener());
		}else{
			mImageLoader.displayImage(mThumbnail.get(position), holder.albumIv,
					GlobalSetting.getWeiboCacheOpt(mContext), GlobalSetting.getImageLoadingListener());
		}
		return convertView;
	}

	public void setData(ArrayList<String> thumbnails,ArrayList<String> exts) {
		mThumbnail = thumbnails;
		mImgExt = exts;
	}

	public void setImgSize(int w, int h) {
		mImgWidth = w;
		mImgHeight = h;
	}

	private static class ViewHolder {
		ImageView albumIv;
		ImageView ivGifSign;
	}
	
	public void setIsLocalImg(boolean flag){
		mIsLocalImg = flag;
	}
}
